// JavaScript Document
/*
======== 省份城市联动js =========
示例数据岛:
<xml id='ProvinceCity'>
<?xml version='1.0' encoding='utf8'?>
<root>
	<def province='湖北省' city='武汉市'/>
	<province name='湖北省'>
		<city name='武汉市'/>
		<city name='宜昌市'/>
		<city name='枝江市'/>
	</province>
	<province name='湖南省'>
		<city name='长沙市'/>
	</province>
</root>
</xml>

页面ActionForm:
<form name="ActionForm" method="post" action="" onSubmit="">

省份<select name="province" id="province" style='width:200px'>
</select>

城市<select name="city" id="city" style='width:200px'>
</select>
</form>

使用方法:
var provCity = new Roly.ProvCity();
document.onreadystatechange = function() {
	if(document.readyState=="complete") {
		provCity.init();
		document.write(provCity.toString());
	}
}
*/

function ProvCity() {
	this.obj = this;
	this.province = "";
	this.city = "";
	this.list = [];
}
function Province() {
	this.name = "";
	this.cityList = [];
}

ProvCity.prototype.parseXml = function() {
	var xml = _$("ProvinceCity");
	if(xml == null || xml == undefined) {
		alert("Error:Can't find xml data!");
		return;
	}
	alert(xml);
	var root = xml.documentElement;
	
	var def = root.firstChild;
	this.province = def.getAttribute("province");
	this.city = def.getAttribute("city");
	
	for(var i=1;i<root.childNodes.length;i++) {
		var pNode = root.childNodes[i];
		var bean = new Province();
		bean.name = pNode.getAttribute("name");

		for(var j=0;j<pNode.childNodes.length;j++) {
			var cNode = pNode.childNodes[j];
			bean.cityList[bean.cityList.length] = cNode.getAttribute("name");
		}

		this.list[this.list.length] = bean;
	}
}

ProvCity.prototype.init = function(p,c) {
	this.parseXml();
	if(p=="null" || c=="null") {
		p = "";
		c = "";
	}
	this.province = p;
	this.city = c;
	var slt_usr = _$("customerBean.provinceId");
	var slt_dpt = _$("customerBean.cityId");
	if(slt_usr == null || slt_dpt == null)
		return;
	slt_usr.length = 0;
	slt_usr.param = this;
	slt_usr.onchange = this.changeProxy;

	var blank = document.createElement("option");
	blank.setAttribute("value","");
	blank.appendChild(document.createTextNode("------"));
	slt_usr.appendChild(blank);

	slt_dpt.length = 0;
	for(var i=0;i<this.list.length;i++) {
		var bean = this.list[i];
		var op = document.createElement("option");
		op.setAttribute("value",bean.name);
		op.appendChild(document.createTextNode(bean.name));
		if(this.province == bean.name)
			op.selected = true;
		slt_usr.appendChild(op);
	}

	if(this.province != "") {
		this.change(this.province,this.city);
	} else {
		this.change(slt_usr.options[0].value,this.city);
	}

}

ProvCity.prototype.changeProxy = function() {
	var S = _$("customerBean.provinceId");
	var obj = S.param;
	var prov_name = S.options[S.selectedIndex].value;
	obj.change(prov_name);
}

//当选择一个省份时，控制该省份的城市变化
ProvCity.prototype.change = function(prov_name,city_name) {
	var slt_prov = _$("customerBean.provinceId");
	var slt_city = _$("customerBean.cityId");
	slt_city.length = 0;
	
	var blank = document.createElement("option");
	blank.setAttribute("value","");
	blank.appendChild(document.createTextNode("------"));
	slt_city.appendChild(blank);
	
	if(prov_name == "") {
		return;
	}

	var cityList = this.getCityList(prov_name);
	for(var i=0;i<cityList.length;i++) {
		var temp = cityList[i];
		var op = document.createElement("option");
		op.setAttribute("value",temp);
		op.appendChild(document.createTextNode(temp));
		if(city_name != undefined && city_name==temp)
			op.selected = true;
		if(this.province == prov_name && this.city == temp)
			op.selected = true;
		slt_city.appendChild(op);
	}
}

ProvCity.prototype.getCityList = function(prov_name) {
	var bean = null;
	for(var i=0;i<this.list.length;i++) {
		var temp = this.list[i];
		if(temp.name == prov_name) {
			bean = temp.cityList;
			break;
		}
	}
	return bean;
}

ProvCity.prototype.toString = function() {
	var s = "";
	s += "Default province:"+this.province+"<br>";
	s += "Default city:"+this.city+"<br>";
	s += "ProvCity list:<br>";
	for(var i=0;i<this.list.length;i++) {
		var temp = this.list[i];
		s += "["+temp.name+"]<br>";
		for(var j=0;j<temp.cityList.length;j++) {
			s += "\t"+temp.cityList[j]+"<br>";
		}
	}
	return s;
}
